package com.bw.day03.fg

import androidx.lifecycle.viewModelScope
import com.bw.base.net.entity.GoodsEntity
import com.bw.base.net.entity.IndexEntity
import com.bw.base.net.entity.KanJiaEntity
import com.bw.base.net.entity.QuanEntity
import com.bw.base.net.entity.Res
import com.bw.base.net.entity.TeamEntity
import com.bw.base.net.mvvm.BaseViewModel
import com.bw.day03.utlis.SingleLiveData
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {
    val indexSuccess=SingleLiveData<Res<IndexEntity>>()
    val indexFail=SingleLiveData<String>()
    fun index(){
        viewModelScope.launch {
            try {
                indexSuccess.postValue(apiService.index())
            }catch (e:Exception){
                indexFail.postValue(e.message.toString())
            }
        }
    }
    val goodsSuccess=SingleLiveData<Res<GoodsEntity>>()
    val goodFail=SingleLiveData<String>()
    fun indexGoods(type:Int,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                goodsSuccess.postValue(apiService.indexGoods(type,limit,page))
            }catch (e:Exception){
                goodFail.postValue(e.message.toString())
            }
        }
    }
    val quanSuccess=SingleLiveData<Res<QuanEntity>>()
    val quanFail=SingleLiveData<String>()
    fun quan(limit:String,page:String,type:String){
        viewModelScope.launch {
            try {
                quanSuccess.postValue(apiService.quan(limit,page, type))
            }catch (e:Exception){
                quanFail.postValue(e.message.toString())
            }
        }
    }
    val receiveSuccess=SingleLiveData<Res<String?>>()
    val receiveFail=SingleLiveData<String>()
    fun quanReceive(couponId:Int){
        viewModelScope.launch {
            try {
                val map=mutableMapOf<String,Any>()
                map["couponId"]=couponId
                receiveSuccess.postValue(apiService.quanReceive(mapToBody(map)))

            }catch (e:Exception){
                receiveFail.postValue(e.message.toString())
            }
        }

    }

    val kanjiaSuccess=SingleLiveData<Res<KanJiaEntity>>()
    val kanjiaFail=SingleLiveData<String>()
    fun kanjia(){
        viewModelScope.launch {
            try {
                kanjiaSuccess.postValue(apiService.kanjia())
            }catch (e:Exception){
                kanjiaFail.postValue(e.message.toString())
            }
        }
    }
    val teamSuccess=SingleLiveData<Res<TeamEntity>>()
    val teamFail=SingleLiveData<String>()
    fun team(limit: Int,page: Int){
        viewModelScope.launch {
            try {
                teamSuccess.postValue(apiService.team(limit,page))
            }catch (e:Exception){
                teamFail.postValue(e.message.toString())
            }
        }
    }
}